{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python3入门到精通实战特训（第19期）第5课书面作业\n",
    "\n",
    "学号：116646\n",
    "\n",
    "**作业内容：**\n",
    "\n",
    "使用协程抓取网络图片。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://kr.shanghai-jiuxin.com/file/bizhi/20220929/h0z4y4ihate.jpg :结束\n",
      "http://kr.shanghai-jiuxin.com/file/bizhi/20220929/iqitunma5f3.jpg :结束\n",
      "http://kr.shanghai-jiuxin.com/file/bizhi/20220929/ptgvyyrnvgn.jpg :结束\n"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "import time\n",
    "import aiohttp\n",
    " \n",
    " \n",
    "urls = [\n",
    "    \"http://kr.shanghai-jiuxin.com/file/bizhi/20220929/ptgvyyrnvgn.jpg\",\n",
    "    \"http://kr.shanghai-jiuxin.com/file/bizhi/20220929/h0z4y4ihate.jpg\",\n",
    "    \"http://kr.shanghai-jiuxin.com/file/bizhi/20220929/iqitunma5f3.jpg\",\n",
    "    ]\n",
    " \n",
    "async def aiodownload(url):\n",
    "    name = url.rsplit('/',1)[1] \n",
    "    async with aiohttp.ClientSession() as session:\n",
    "        async with session.get(url) as resp: \n",
    "            with open(name ,mode='wb') as f:\n",
    "                f.write(await resp.content.read()) \n",
    "    print(url,':结束')\n",
    "\n",
    "tasks = [asyncio.create_task(aiodownload(url)) for url in urls]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "1cca9558bc5ad879ec93cc030b157d75f18267527c60932cecaace349eef54dd"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
